package cn.readjava.controller.manage;

import cn.readjava.bean.User;
import cn.readjava.constant.ShiroConstant;
import cn.readjava.service.UserService;
import cn.readjava.util.ResponseResult;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/manage/user")
public class ManageUserController {

    @Resource
    private UserService userService;

    // 重置密码：参数:id,password,email
    @PostMapping("/resetPwd")
    @ResponseBody
    public Object resetPwd(User us) {
        User dbUser = userService.getOne(us.getUserId());
        Md5Hash md5Pwd = new Md5Hash(us.getUserPassword(), us.getUserEmail() + ShiroConstant.SALT, ShiroConstant.HASHITERATIONS);
        dbUser.setUserPassword(md5Pwd.toString());
        User save = userService.save(dbUser);
        if (save != null) {
            return new ResponseResult(200, "重置成功", null);
        }
        return new ResponseResult(100, "重置失败", null);
    }

    // 删除
    @GetMapping("/delete/{id}")
    @ResponseBody
    public Object delete(@PathVariable Long id) {
        try {
            userService.deleteById(id);
            return new ResponseResult(200, "删除成功", null);
        } catch (Exception e) {
            return new ResponseResult(100, "删除失败", null);
        }
    }

    /*@GetMapping("/list")
    public String list(Model model) {
        List<User> userList = userService.findAllByLevelId(0);
        model.addAttribute("userList", userList);
        return "manage/manage-user";
    }*/
}
